package com.lincoln.cloud.common.utils;

/**
 * 字节集工具
 *
 * @author lincoln
 * @date 2019/1/11
 */
public class ByteUtil {
    /**
     * 连接两个数组
     *
     * @param ts
     * @return
     */
    public static byte[] append(byte[]... ts) {
        int length = 0;
        for (byte[] t : ts) {
            length += t == null ? 0 : t.length;
        }
        byte[] res = new byte[length];
        int i = 0;
        for (byte[] t : ts) {
            if (t != null) {
                for (byte b : t) {
                    res[i++] = b;
                }
            }
        }
        return res;
    }

    /**
     * 从指定位置截取字符串
     *
     * @param bytes
     * @param beginIndex
     * @return
     */
    public static byte[] sub(byte[] bytes, int beginIndex) {
        return sub(bytes, beginIndex, bytes.length);
    }

    /**
     * 从指定位置截取字符串到指定位置
     *
     * @param bytes
     * @param beginIndex
     * @return
     */
    public static byte[] sub(byte[] bytes, int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new IndexOutOfBoundsException("String index out of range: " + beginIndex);
        } else if (endIndex > bytes.length) {
            throw new IndexOutOfBoundsException("String index out of range: " + endIndex);
        } else {
            int length = endIndex - beginIndex;
            if (length < 0) {
                throw new IndexOutOfBoundsException("String index out of range: " + length);
            } else {
                byte[] res = new byte[length];
                for (int i = 0; i < length; i++) {
                    res[i] = bytes[beginIndex + i];
                }
                return res;
            }
        }
    }


    /**
     * int转byte数组,int等于4个byte
     *
     * @param num
     * @return
     */
    public static byte[] int2byte(int num) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) ((num >> 24) & 0xff);
        bytes[1] = (byte) ((num >> 16) & 0xff);
        bytes[2] = (byte) ((num >> 8) & 0xff);
        bytes[3] = (byte) (num & 0xff);
        return bytes;
    }

    /**
     * byte数组转int类型的对象,int等于4个byte
     *
     * @param bytes
     * @return
     */
    public static int byte2int(byte[] bytes) {
        return (bytes[0] & 0xff) << 24
                | (bytes[1] & 0xff) << 16
                | (bytes[2] & 0xff) << 8
                | (bytes[3] & 0xff);
    }

    /**
     * 比较两个字节数组是否相同
     * @param a
     * @param b
     * @return
     */
    public static boolean compare(byte[] a, byte[] b) {
        if (a == null) {
            if (b == null) {
                return true;
            } else {
                return false;
            }
        }
        if (b == null) {
            return false;
        }
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(byte2int(int2byte(999999999)));
    }
}
